﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using HellCook.Model;

namespace HellCook.WinApp_WPF.Menu
{
    /// <summary>
    /// Interaction logic for AgregarRecetaAMenuSemanal.xaml
    /// </summary>
    public partial class AgregarRecetaAMenuSemanal : Window
    {
        private Receta RECETA;
        private TextBox textBox;
        public bool Ejecutar { get; set; }

        public AgregarRecetaAMenuSemanal()
        {
            InitializeComponent();

            ObtenerTodasLasRecetas();
        }

        private void ObtenerTodasLasRecetas()
        {
            this.dataGrid_ResultSearch.ItemsSource = HellCook.Business.Receta_Buscador.ObtenerTodasLasRecetas();
        }

        public AgregarRecetaAMenuSemanal(Receta RECETA, TextBox textBox)
        {
            InitializeComponent();

            Ejecutar = false;
            this.textBox_NameRecipe.Text = textBox.Text;            
            this.RECETA = RECETA;
            this.textBox = textBox;

            RECETA = HellCook.Business.Receta_Buscador.ObtenerReceta(this.textBox_NameRecipe.Text);
            if (RECETA == null)
            {
                Ejecutar = true;
            }
        }                

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Agregar();
        }

        private void textBox_Namerecipe_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (String.IsNullOrEmpty(this.textBox_NameRecipe.Text))
            {
                ObtenerTodasLasRecetas();
            }
            else
            {
                this.dataGrid_ResultSearch.ItemsSource = null;
                this.dataGrid_ResultSearch.ItemsSource = HellCook.Business.Receta_Buscador.ObtenerTodosLasRecetasSimilarA(this.textBox_NameRecipe.Text);
            }
        }

        private void dataGrid_ResultSearch_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.dataGrid_ResultSearch.SelectedIndex >= 0)
            {
                Receta RecetaSeleccionada = (Receta)this.dataGrid_ResultSearch.SelectedItem;
                this.dataGrid_ListaIngredientes.ItemsSource = RecetaSeleccionada.Lista_Ingredientes;

                Receta receta = (Receta)this.dataGrid_ResultSearch.SelectedItem;
                this.textBox_NameRecipe.Text = receta.Nombre_Receta;
                this.RECETA = receta;
            }
            else
            {
                this.dataGrid_ListaIngredientes.ItemsSource = null;
            }
        }

        private void Agregar()
        {
            if (String.IsNullOrEmpty(this.textBox_NameRecipe.Text))
            {
                MessageBox.Show("El campo del nombre del receta no puede estar vacio", "Mensaje");
            }
            else
            {
                this.RECETA = HellCook.Business.Receta_Buscador.ObtenerReceta(this.textBox_NameRecipe.Text);
                if (RECETA == null)
                {
                    MessageBoxResult result;
                    result = MessageBox.Show("La receta no existe.\n¿Desea agregar?", "Mensaje", MessageBoxButton.YesNo);
                    if (result == MessageBoxResult.Yes)
                    {
                        HellCook.WinApp_WPF.AddNewRecipe NuevaReceta = new AddNewRecipe(this.textBox_NameRecipe.Text);
                        NuevaReceta.ShowDialog();
                    }
                }
                else
                {
                    RECETA.Nombre_Receta = RECETA.Nombre_Receta.Replace(" ", "");
                    this.textBox.Text = textBox_NameRecipe.Text;
                    this.Close();
                }
            }
        }

        private void MouseDoubleClick_AgregarReceta(object sender, MouseButtonEventArgs e)
        {
            Agregar();
        }
    }
}
